package com.galeno.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.*;

/**
 * @author galeno
 * @Title:
 * @Description:
 * @date 2021/7/2216:54
 */
public class HdfsIO {
    public static void main(String[] args) throws IOException, InterruptedException {
        HdfsIO hdfsIO=new HdfsIO();
        hdfsIO.fileWrite();
        Thread.sleep(1000);
        hdfsIO.fileRead();


    }

    /**
     * 创建文本文件写入到hdfs
     * @throws IOException
     */
    public void fileWrite() throws IOException {
        FileSystem fs = getFileSystem();
        FSDataOutputStream fsDataOutputStream = fs.create(new Path("/中国共产党成立100周年.txt"));
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fsDataOutputStream));
        int year=100;
        for (int i = 1; i <=year; i++) {
            bufferedWriter.write("中国共产党"+i+"岁了");
            bufferedWriter.newLine();
            if(i==100){
                bufferedWriter.write("不忘初心,牢记使命,为社会主义而奋斗,以习近平新时代中国特色社会主义思想作为指导思想.");
            }
        }
        System.out.println("文件创建成功");
        bufferedWriter.close();
        fsDataOutputStream.close();
        fs.close();
    }

    /**
     * 从hdfs读取文本文件并打印
     * @throws IOException
     */
    public void fileRead() throws IOException {
        FileSystem fs = getFileSystem();
        FSDataInputStream fsDataInputStream = fs.open(new Path("/中国共产党成立100周年.txt"));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fsDataInputStream));
        String str=null;
        while ((str=bufferedReader.readLine())!=null){
            System.out.println(str);
        }
        bufferedReader.close();
        fsDataInputStream.close();
        fs.close();






    }
    /**
     * 获得FileSystem对象
     */
    public FileSystem getFileSystem() throws IOException {
        return FileSystem.get(new Configuration());
    }


}
